package com.tedu.coolshark.controller;

import com.tedu.coolshark.entity.User;
import com.tedu.coolshark.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class UserController {
    @Autowired(required = false)
    UserMapper mapper;
    @RequestMapping("/login")
    public int login(@RequestBody User user,HttpSession session){
        User u = mapper.selectByUsername(user.getUsername());
        if (u!=null){
            if (u.getPassword().equals(user.getPassword())){
                //把当前登录成功的用户对象保存到会话对象
                session.setAttribute("user",u);
                //1.54
                return 1;//登录成功
            }
            return 3;//密码错误
        }
        return 2;//用户名不存在
    }


    @RequestMapping("/currentUser")
    public User currentUser(HttpSession session){
        //把登录成功时保存的用户对象取出  返回给客户端
        return(User) session.getAttribute("user");

    }
    @RequestMapping("/logout")
    public void logout(HttpSession session){
        //把登录成功时保存的用户对象删除
        session.removeAttribute("user");

    }

}
